package com.ceit.service.impl;

import com.ceit.dao.AuthorityMapper;
import com.ceit.dao.RolesAuthorityMapper;
import com.ceit.dao.UserMapper;
import com.ceit.dao.UserRoleMapper;


import com.ceit.model.*;
import com.ceit.service.loginS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 隼 on 2017/1/22.
 */
@Service
public class loginSImpl implements loginS {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RolesAuthorityMapper rolesAuthorityMapper;
    @Autowired
    private AuthorityMapper authorityMapper;

    @Override
    public int getRole(Integer id) throws Exception {
        UserRole userRole=userRoleMapper.selectByPrimaryKey(id);
        if(userRole==null){
            return  5;
        }else {
        return  userRoleMapper.selectByPrimaryKey(id).getRoleId();
        }
    }

    @Override
    public String getPassord( Integer id) throws Exception {
        User user=null;
        user=userMapper.selectByPrimaryKey(id);
        if(user==null){
            return "";
        }else {
            return user.getUserPassword();
        }
    }

    //获取user_id
    @Override
    public int getID( String str) throws Exception {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andNumEqualTo(str);
        List<User> user=  userMapper.selectByExample(userExample);
       int id=-1;    //-1表示id不存在
        Iterator<User> iterator=user.iterator();
        if(iterator.hasNext()){
            id=iterator.next().getUserId();
        }
        return id;
    }

    @Override
    public List<String> getAuthorityByRole(int id) throws Exception {
        RolesAuthorityExample rolesAuthorityExample=new RolesAuthorityExample();
        rolesAuthorityExample.createCriteria().andRoleIdEqualTo(id);
        List<RolesAuthority> list=rolesAuthorityMapper.selectByExample(rolesAuthorityExample);
        List<String> stringList=new ArrayList<>();
        Iterator<RolesAuthority> iterator=list.iterator();
        while(iterator.hasNext()){
            int authorityId=iterator.next().getAuthorityId();
            stringList.add(authorityMapper.selectByPrimaryKey(authorityId).getAuthority());
        }
        return stringList;
    }



}
